#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"

void STInit(ST* ps)
{
	assert(ps);

	ps->data = NULL;
	ps->top = -1;
	ps->capacity = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->data);
	ps->data = NULL;
	ps->top = -1;
	ps->capacity = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);

	//CheckCapacity
	if (ps->capacity == ps->top + 1)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
		if (NULL == tmp)
		{
			perror("realloc failed");
			exit(-1);
		}

		ps->data = tmp;
		ps->capacity = newcapacity;
	}
	ps->top++;
	ps->data[ps->top] = x;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top >= 0);

	ps->top--;
}

STDataType STTop(ST* ps)
{
	assert(ps);

	return ps->data[ps->top];
}

int STSize(ST* ps)
{
	assert(ps);

	return( ps->top + 1);
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == -1;
}